package org.core.tliaswebmanagement.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.core.tliaswebmanagement.anno.LogOperation;
import org.core.tliaswebmanagement.mapper.ClazzMapper;
import org.core.tliaswebmanagement.pojo.Clazz;
import org.core.tliaswebmanagement.pojo.ClazzQueryParam;
import org.core.tliaswebmanagement.pojo.Emp;
import org.core.tliaswebmanagement.pojo.PageResult;
import org.core.tliaswebmanagement.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    @Override
    public List<Clazz> findAll() {return clazzMapper.findAll();}

    @Override
    public PageResult<Clazz> page(ClazzQueryParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<Clazz> clazzlist = clazzMapper.list(param);
        //设置班级状态，显示为：未开班、已结课、在读中 这三种。
        //如果：
        //  - 当前时间 > 结课时间：状态未 已结课。
        //  - 当前时间 < 开课时间：状态未 未开班。
        //  - 否则，就是 在读中。
        for (Clazz clazz : clazzlist) {
            if (clazz.getEndDate().isBefore(LocalDate.now())) {
                clazz.setStatus("已结课");
            } else if (clazz.getBeginDate().isAfter(LocalDate.now())) {
                clazz.setStatus("未开班");
            } else {
                clazz.setStatus("在读中");
            }
        }
        Page<Clazz> pageInfo = (Page<Clazz>) clazzlist;
        return new PageResult<Clazz>(pageInfo.getTotal(), pageInfo.getResult());
    }

    @Override
    public void deleteById(Integer id) {clazzMapper.deleteById(id);}

    @Override
    public void add(Clazz clazz) {clazzMapper.insert(clazz);}

    @Override
    public void update(Clazz clazz) {clazzMapper.updateById(clazz);}
    @LogOperation
    @Override
    public Clazz getInfo(Integer id) {return clazzMapper.getById(id);}
}
